<template>
  <div>
    <el-dialog :title="`${$t('basic.stage')}：PH_DATA_PRE`" :visible.sync="isShowPRE" :close-on-click-modal="false"
      :before-close="handleClose" :width="screenWidth > 1310 ? '55%' : '70%'" v-dialogDrag>
      <el-form :model="formData" label-width="70px" ref="ruleForm" size="mini" :rules="rules" :hide-required-asterisk="true">
        <el-row :gutter="24" class="gz-form-row">
         
          <el-col :xs="5" :sm="4" :md="4" :lg="3" :xl="3">
            <el-form-item label="" label-width="0">
              <el-checkbox v-model="formData.isAuto">{{$t('common.auto_start')}}</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="10" :md="8" :lg="8" :xl="5">
            <el-form-item :label="$t('common.start_delay')" label-width="80px" class="inputPadding">
              <el-input type="number" v-model="formData.delayTime">
                <template slot="append">min</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="5" :sm="4" :md="4" :lg="3" :xl="3">
            <el-form-item label="" label-width="0">
              <el-checkbox v-model="formData.mode">{{$t('common.operation_signature')}}</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :xs="5" :sm="5" :md="5" :lg="3" :xl="3">
            <el-form-item label="" label-width="0">
              <el-checkbox v-model="formData.checks">{{$t('common.review_confirmation')}}</el-checkbox>
            </el-form-item>
          </el-col>
          <!-- <el-col :xs="2" :sm="2" :md="1" :lg="3" :xl="3">
                        <el-form-item label="">

                        </el-form-item>
                    </el-col>
                    <el-col :xs="12" :sm="12" :md="8" :lg="8" :xl="8">
                        <el-form-item :label="$t('basic.stage_name')" prop="phaseDesc">
                            <el-input disabled v-model="formData.phaseDesc" placeholder="" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="12" :sm="6" :md="2" :lg="2" :xl="2">
                        <el-form-item :label="$t('common.enable')">
                            <el-checkbox v-model="formData.isAuto" @change="changeIsActive"></el-checkbox>
                        </el-form-item>
                    </el-col> -->
          <!-- <el-col :xs="12" :sm="6" :md="5" :lg="6" :xl="4">
                        <el-form-item label="数字签名" label-width="115px">
                            <el-checkbox v-model="formData.signitature" @change="changeIsActive"></el-checkbox>
                        </el-form-item>
                    </el-col> -->
        </el-row>
        <el-row :gutter="24">
          <el-col :xs="8" :sm="8" :md="8" :lg="8" :xl="8">
            <el-form-item label="压力上限" prop="hi">
              <el-input type="number" v-model="formData.hi" placeholder="" keyup.native="isFloor" clearable>
                <template slot="append">MPa</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="8" :sm="8" :md="8" :lg="8" :xl="8">
            <el-form-item label="压力下限" prop="lo">
              <el-input type="number" v-model="formData.lo" placeholder="" keyup.native="isFloor" clearable>
                <template slot="append">MPa</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="35">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label-width="0">
              <!-- <el-checkbox v-model="formData.confirm" @change="changeIsActive"> -->
              <p style="font-weight: bold">{{$t('recipe.phase_info')}}</p>
              <!-- </el-checkbox> -->
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item :label="$t('common.operating_instructions')">
              <el-input rows="3" type="textarea" v-model="formData.guideInfo" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item :label="$t('basic.remarks')">
              <el-input rows="3" type="textarea" v-model="formData.flastModifyComment" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" :disabled="isreadOnly" :loading="loading"
          @click="handleConfirm('ruleForm')">{{ $t("common.preservation") }}</el-button>
        <el-button @click="handleClose()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { formula } from "@/api/formula"
export default {
  props: {
    isShowPRE: Boolean,
    tdIndex: Number,
    stepObj: Object,
    isreadOnly: Boolean
  },
  data () {
    let LoFn = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入下限'));
      } else if (parseFloat(value) > parseFloat(this.formData.hi)) {
        callback(new Error('压力下限不能高于压力上限'));
      } else {
        callback()
      }
    }
    return {
      screenWidth: document.body.clientWidth,
      formData: {
        seqNo: "",
        delayTime: "",
        mode: "",
        checks: "",
        stepNo: "",
        stepDesc: "",
        isActive: false,
        phaseDesc: "压力配置",
        confirm: "",
        flastModifyComment: "",
        guideInfo: "",
        isAuto: false,
        hi: "",
        lo: "",
        stepSort: 0,
      },
      rules: {
        hi: [{ required: true, message: '请输入压力上限', trigger: 'blur' }],
        lo: [{ validator: LoFn, trigger: 'blur' }]
      },
      loading: false,
      phaseId: "",
      title: "添加",
      stepId1: "",
      stepId2: ""
    }
  },
  watch: {
    screenWidth (val) {
      this.screenWidth = Number(val)
    },
    tdIndex (val) {
      this.formData.seqNo = val
    },
    isShowPRE (val) {
      if (val) {
        this.formData.confirm = this.stepObj.confirm == 1 ? true : false
        this.formData.flastModifyComment = this.stepObj.flastModifyComment
        this.formData.guideInfo = this.stepObj.guideInfo
        this.formData.isAuto = this.stepObj.isAuto == 1 ? true : false

        this.formData.stepNo = this.tdIndex
        this.formData.mode = this.stepObj.mode == 1 ? true : false
        this.formData.checks = this.stepObj.checks == 1 ? true : false
        this.formData.delayTime = this.stepObj.delayTime



        // this.formData.signitature = this.stepObj.signitature  == 1 ? true : false
        this.formData.phaseDesc = this.stepObj.texts
        this.phaseId = this.stepObj.fid || this.stepObj.fId
        this.formData.seqNo = this.tdIndex
        this.formData.stepSort = this.stepObj.stepSort ?? 0
        if (this.phaseId) {
          this.phaseDataList()
        }
      } else {
        for (let key in this.formData) {
          this.formData[key] = ""
        }
        this.phaseId = ""
        this.title = "添加"
        this.stepId1 = ""
        this.stepId2 = ""
      }
    }
  },
  mounted () {
    const that = this
    window.addEventListener('resize', function () {
      that.screenWidth = document.body.offsetWidth;
    })
  },
  created () {

  },
  methods: {
    phaseDataList () {
      const param = {
        phaseId: this.phaseId
      }
      formula.phaseDataList(param).then(res => {
        if (res.data.rows.length > 0) {
          this.title = "编辑"
          this.formData.hi = res.data.rows[0].target
          this.formData.lo = res.data.rows[1].target
          this.stepId1 = res.data.rows[0].fid
          this.stepId2 = res.data.rows[1].fid
        } else {
          this.title = "添加"
          this.formData.hi = ''
          this.formData.lo = ''
          this.stepId1 = ''
          this.stepId2 = ''
        }
      })
    },
    handleConfirm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = {
            phaseDesc: this.formData.phaseDesc,
            isAuto: this.formData.isAuto == false ? 0 : 1,
            isActive: this.formData.isActive == false ? 0 : 1,
            checks: this.formData.checks == false ? 0 : 1,
            delayTime: this.formData.delayTime,
            operationId: this.stepObj.operationId,
            type: this.stepObj.type,
            confirm: this.formData.confirm == false ? 0 : 1,
            flastModifyComment: this.formData.flastModifyComment,
            guideInfo: this.formData.guideInfo,
            mode: this.formData.mode == false ? 0 : 1,
            stepNo: this.stepObj.stepNo,
            seqNo: this.stepObj.seqNo,
            phaseCode: this.stepObj.phaseCode,
            serialNo: this.stepObj.serialNo,
            stepSort: this.formData.stepSort,
            // signitature:this.formData.signitature == false ? 0 : 1,
          }
          if (this.phaseId) {
            param.fid = this.phaseId
          }
          this.loading = true
          formula.phaseAdd(param).then(res => {
            if (res.code == 200) {
              this.phaseDataAdds()
            } else {
              this.loading = false
              this.$message.error(res.msg);
            }
          })
        }
      })
    },
    phaseDataAdds () {
      const param = [{
        paraName: "温度上限",
        uom: "℃",
        target: this.formData.hi,
        phaseId: this.phaseId,
      }, {
        paraName: "温度下限",
        uom: "℃",
        target: this.formData.lo,
        phaseId: this.phaseId,
      }]
      if (this.title == "编辑") {
        param[0].fid = this.stepId1
        param[1].fid = this.stepId2
      }
      formula.phaseDataAdds(param, this.title).then(res => {
        this.loading = false
        if (res.code == 200) {
          let obj = {
            mode: this.stepObj.mode,
            isAuto: this.formData.isAuto == false ? 0 : 1,
            isActive: this.formData.isActive == false ? 0 : 1,
            operationId: this.stepObj.operationId,
            seqNo: this.stepObj.seqNo,
            serialNo: this.stepObj.serialNo,
            text: this.stepObj.phaseDesc
          }
          this.$emit("uptionData", obj)
          this.$emit("closePH", "PRE")
        }
      })
    },
    handleClose () {
      this.$refs["ruleForm"].resetFields();
      this.$emit("closePH", "PRE")
    },
    changeIsActive (val) {
      this.$forceUpdate()
    },
    isFloor () {
      var obj = event.target;
      var t = obj.value.charAt(0);
      obj.value = obj.value.replace(".", "$#$")//把第一个字符'.'替换成'$#$'
        .replace(/\./g, "")//把其余的字符'.'替换为空
        .replace("$#$", ".")//把字符'$#$'替换回原来的'.'
        .replace(/[^\d.]/g, "")//只能输入数字和'.'
        .replace(/^\./g, "")//不能以'.'开头
        .replace(/([0-9]+\.[0-9]{2})[0-9]*/, "$1")//只保留2位小数
      if (t == '-') {
        obj.value = '-' + obj.value;
      }
    }
  }
}
</script>
